<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button type="warning" @click="addCategory" icon="el-icon-plus">添加分类</el-button></div>
      <div class="ml-2"><el-button type="danger" icon="el-icon-delete">批量删除</el-button></div>
    </div>
    <el-table :data="categoriesList" border stripe>
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="65" />
      <el-table-column align="center" label="分类名称" prop="name" width="130" />
      <el-table-column align="center" label="图标" prop="icon_font" width="95" />
      <el-table-column align="center" label="上级分类" prop="parent_name" width="65" />
      <el-table-column align="center" label="排序" prop="sort" width="45" />
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-1 mt-3">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="添加分类">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="名称" prop="name">
          <div style="width: 50%;">
            <el-input v-model="form.name"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <div style="width: 18%;">
            <el-input v-model="form.sort"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="父分类" prop="parent">
          <div style="width: 30%;">
            <el-select v-model="form.parent" placeholder="请选择">
              <el-option v-for="item in categoriesList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="icon" prop="icon_font">
          <div style="width: 30%;">
            <el-input v-model="form.icon_font"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    articlecategoryListUrl,
    articlecategoryDetailUrl,
    articlecategoryCreateUrl,
    articlecategoryUpdateUrl,
    articlecategoryDeleteUrl
  } from '@/utils/news'
  export default {
    name: 'category',
    data() {
      return {
        categoriesList: [],
        dialogVisible: false,
        addStatu: true,
        form: {
          name: '',
          sort: 1,
          parent: '',
          icon_font: '',
          cover_image: ''
        },
        props: {
          value: 'id',
          label: 'name',
        },
        rules: {
          name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
        getCategoryId:'',
        queryInfo: {
          page: 1,
          pageSize: 10
        },
        total: 0
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getCategoriesList()
      },
      getCategoriesList() {
        this.$get(articlecategoryListUrl, this.queryInfo).then(res => {
          this.categoriesList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.pageSize = newSize
        this.getCategoriesList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getCategoriesList()
      },
      addCategory() {
        this.dialogVisible = true
        this.addStatu = true
      },
      handleCascaderChange(value) {
        // 将选中的值转换为字符串类型
        this.form.parent = value.join(',');
      },
      // 提交表单
      submitForm() {
        if(this.addStatu){
          this.$post(articlecategoryCreateUrl, this.form).then(res => {
              this.getCategoriesList()
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.form = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        }else{
          this.$put(articlecategoryUpdateUrl + this.getCategoryId + '/', this.form).then(res => {
              this.getCategoriesList()
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.form = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('修改失败')
            })
        }

      },
      openDelete(catagoryId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.handleDelete(catagoryId);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDelete(categoryId) {
        console.log(categoryId);
        this.$del(articlecategoryDeleteUrl + categoryId + '/').then(res => {
          this.getCategoriesList()
        })
      },
      handleEdit(categoryId) {
        this.dialogVisible = true
        this.addStatu = false
        this.getCategoryId = categoryId
        this.$get(articlecategoryDetailUrl + categoryId + '/').then(res => {
          if (res.data.code == 200) {
            this.form = {
              name: res.data.data.name,
              sort: res.data.data.sort,
              parent: res.data.data.parent,
              icon_font: res.data.data.icon_font,
              cover_image: res.data.data.cover_image
            }
          }
        })
      }
    }
  }
</script>
